package cn.tedu.javagame.FirePlane;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class ShootPanel extends JPanel implements MouseMotionListener {
    Image bgimage = new ImageIcon("plane/background.jpg").getImage();
    int bgY = 0;
    // 定义滚动速度
    public static final int SCROLL_SPEED = 2;
    // 定时器
    public Timer timer;
    // 英雄机属性
    Hero hero = new Hero();
    // 敌机列表
    List<Enemy> enemies = new ArrayList<>();
    List<Bullet> bullets = new ArrayList<>();
    List<enemyBullet> enemyBullets = new ArrayList<>(); // 敌机子弹列表
    List<Heart> hearts = new ArrayList<>(); // 添加 Hearts 列表
    int index = 0;
    boolean gameOver = false;

    public ShootPanel() {
        this.addMouseMotionListener(this);
    }

    public void paint(Graphics g) {
        super.paint(g);
        if (gameOver) {
            g.setColor(Color.RED);
            g.setFont(new Font("宋体", Font.BOLD, 50));
            g.drawString("游戏结束", ShootFrame.WIDTH / 2 - 100, ShootFrame.HEIGHT / 2);
            return;
        }
        // 绘制背景图片
        paintBackground(g);
        // 绘制英雄机
        paintHero(g);
        // 绘制分数、生命值等
        paintScore(g);
        // 绘制子弹
        paintBullet(g);
        // 绘制敌机
        paintEnemies(g);
        // 绘制敌机子弹
        paintEnemyBullets(g);
        // 绘制 Hearts
        paintHearts(g);
    }

    Image gg = new ImageIcon("plane/gg.png").getImage();

    public void paintScore(Graphics g) {
        g.drawImage(gg, 5, 660, null);
        g.setColor(Color.red);
        // Font的三个参数分别是字体名称、加粗和字体大小
        Font font = new Font("宋体", Font.BOLD, 20);
        g.setFont(font);
        g.drawString("分数：" + hero.score, 360, 710);
        g.drawString("生命：" + hero.life, 210, 710);
        g.drawString("金币：" + hero.gold, 80, 710);
        g.setColor(new Color(107, 255, 96));
        g.drawString("关卡：" + (hero.score / 1000 + 1), 390, 30);
    }

    // 绘制背景图片
    public void paintBackground(Graphics g) {
        g.drawImage(bgimage, 0, bgY, null);
        // 为了实现无缝滚动，再绘制一张图片在上方
        g.drawImage(bgimage, 0, bgY - bgimage.getHeight(null), null);
    }

    public void backgroundMove() {
        bgY += SCROLL_SPEED;
        if (bgY >= ShootFrame.HEIGHT) {
            bgY = 0;
        }
    }

    public void paintHero(Graphics g) {
        g.drawImage(hero.heroImg, hero.x, hero.y, null);
    }

    public void action() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                if (gameOver) {
                    return;
                }
                // 背景图片滚动
                backgroundMove();

                bulleAction();
                bulletMove();
                bulletOutOfBounds();

                moveEnemies();
                enemiesAction();
                enemyOutOfBounds();

                moveEnemyBullets();
                enemyBulletOutOfBounds();

                moveHearts(); // 更新 Hearts 的位置
                heartOutOfBounds(); // 移除越界的 Hearts

                checkCollisions();

                repaint();
            }
        }, 10, 20);
        timer.schedule(new TimerTask() {
            public void run() {
                if (gameOver) {
                    return;
                }
                // 背景图片滚动
                heroFly();
                repaint();
            }
        }, 10, 100);
        // 定时生成 Hearts
        timer.schedule(new TimerTask() {
            public void run() {
                if (gameOver) {
                    return;
                }
                hearts.add(new Heart());
            }
        }, 10, 3000); // 每3秒生成一个 Heart
    }

    // 产生子弹
    int bulletIndex = -1;

    public void bulleAction() {
        bulletIndex++;
        if (bulletIndex == Integer.MAX_VALUE) {
            bulletIndex = -1;
        }
        if (bulletIndex % 15 == 0) {
            bullets.addAll(hero.shoot());
        }
    }

    // 子弹移动
    public void bulletMove() {
        for (Bullet bullet : bullets) {
            bullet.move();
        }
    }

    // 绘制子弹
    public void paintBullet(Graphics g) {
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            g.drawImage(bullet.bulletImg, bullet.x, bullet.y, null);
        }
    }

    // 子弹越界处理
    public void bulletOutOfBounds() {
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            if (bullet.y < 0) {
                bullets.remove(i);
            }
        }
    }

    // 英雄机飞行
    public void heroFly() {
        index++;
        //  切换图片
        if (index >= hero.heroImgs.size()) {
            index = 0;
        }
        // 设置英雄机图片
        hero.heroImg = hero.heroImgs.get(index);
    }

    // 产生敌机
    int enemyIndex = -1;

    public void enemiesAction() {
        enemyIndex++;
        if (enemyIndex % 30 == 0) {
            enemies.add(new Enemy());
        }
    }

    // 绘制敌机
    public void paintEnemies(Graphics g) {
        for (int i = 0; i < enemies.size(); i++) {
            Enemy enemy = enemies.get(i);
            g.drawImage(enemy.enemyImg, enemy.x, enemy.y, null);
        }
    }

    // 敌机移动和动态切换
    public void moveEnemies() {
        for (int j = 0; j < enemies.size(); j++) {
            Enemy enemy = enemies.get(j);
            enemy.move();
            enemy.step();
            // 添加敌机发射的子弹到 enemyBullets 列表
            enemyBullets.addAll(enemy.enemyshoot());
        }
    }

    public void enemyOutOfBounds() {
        for (int i = 0; i < enemies.size(); i++) {
            Enemy enemy = enemies.get(i);
            if (enemy.y > ShootFrame.HEIGHT) {
                // 移除敌机发射的所有子弹
                Iterator<enemyBullet> iterator = enemyBullets.iterator();
                while (iterator.hasNext()) {
                    enemyBullet bullet = iterator.next();
                    if (bullet.shooter == enemy) {
                        iterator.remove();
                    }
                }
                enemies.remove(i);
            }
        }
    }

    // 绘制敌机子弹
    public void paintEnemyBullets(Graphics g) {
        for (int i = 0; i < enemyBullets.size(); i++) {
            enemyBullet bullet = enemyBullets.get(i);
            g.drawImage(bullet.bulletImg, bullet.x, bullet.y, null);
        }
    }

    // 敌机子弹移动
    public void moveEnemyBullets() {
        for (enemyBullet bullet : enemyBullets) {
            bullet.move();
        }
    }

    // 敌机子弹越界处理
    public void enemyBulletOutOfBounds() {
        for (int i = 0; i < enemyBullets.size(); i++) {
            enemyBullet bullet = enemyBullets.get(i);
            if (bullet.y > ShootFrame.HEIGHT) {
                enemyBullets.remove(i);
            }
        }
    }

    // 绘制 Hearts
    public void paintHearts(Graphics g) {
        for (Heart heart : hearts) {
            g.drawImage(heart.img, heart.x, heart.y, null);
        }
    }

    // Hearts 移动
    public void moveHearts() {
        for (Heart heart : hearts) {
            heart.move();
            heart.step();
        }
    }

    // Hearts 越界处理
    public void heartOutOfBounds() {
        for (int i = 0; i < hearts.size(); i++) {
            Heart heart = hearts.get(i);
            if (heart.y > ShootFrame.HEIGHT) {
                hearts.remove(i);
            }
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        if (gameOver) {
            return;
        }
        // 获取鼠标的坐标
        int mouseX = e.getX();
        int mouseY = e.getY();

        // 计算英雄机的新位置，使英雄机的中心与鼠标位置重合
        int newX = mouseX - hero.heroImg.getWidth(null) / 2;
        int newY = mouseY - hero.heroImg.getHeight(null) / 2;

        // 更新英雄机的位置
        hero.move(newX, newY);
    }

    // 碰撞检测
    public void checkCollisions() {
        // 英雄机子弹与敌机的碰撞检测
        for (int i = 0; i < bullets.size(); i++) {
            Bullet heroBullet = bullets.get(i);
            for (int j = 0; j < enemies.size(); j++) {
                Enemy enemy = enemies.get(j);
                if (heroBullet.hitEnemy(enemy)) {
                    enemy.life--;
                    bullets.remove(i);
                    if (enemy.life <= 0) {
                        // 移除敌机发射的所有子弹
                        Iterator<enemyBullet> iterator = enemyBullets.iterator();
                        while (iterator.hasNext()) {
                            enemyBullet bullet = iterator.next();
                            if (bullet.shooter == enemy) {
                                iterator.remove();
                            }
                        }
                        enemies.remove(j);
                        hero.score += 10; // 每消失一架敌机，分数增加10分
                    }
                    break;
                }
            }
        }

        // 英雄机与敌机的碰撞检测
        for (int i = 0; i < enemies.size(); i++) {
            Enemy enemy = enemies.get(i);
            if (hero.hitEnemy(enemy)) {
                // 移除敌机发射的所有子弹
                Iterator<enemyBullet> iterator = enemyBullets.iterator();
                while (iterator.hasNext()) {
                    enemyBullet bullet = iterator.next();
                    if (bullet.shooter == enemy) {
                        iterator.remove();
                    }
                }
                hero.life -= 10;
                enemies.remove(i);
                if (hero.life <= 0) {
                    gameOver = true;
                }
                break;
            }
        }

        // 敌机子弹与英雄机的碰撞检测
        for (int i = 0; i < enemyBullets.size(); i++) {
            enemyBullet enemyBullet = enemyBullets.get(i);
            if (enemyBullet.hitHero(hero)) {
                hero.life -= 10;
                enemyBullets.remove(i);
                if (hero.life <= 0) {
                    gameOver = true;
                }
                break;
            }
        }

        // 英雄机与 Hearts 的碰撞检测
        for (int i = 0; i < hearts.size(); i++) {
            Heart heart = hearts.get(i);
            if (heart.hitHero(hero)) {
                hero.hitHeart(heart);
                hearts.remove(i);
            }
        }
    }
}